package com.hand.demo.api.controller.v1;

import io.choerodon.core.oauth.CustomUserDetails;
import io.choerodon.core.oauth.DetailsHelper;
import org.hzero.boot.scheduler.infra.annotation.JobHandler;
import org.hzero.boot.scheduler.infra.enums.ReturnT;
import org.hzero.boot.scheduler.infra.handler.IJobHandler;
import org.hzero.boot.scheduler.infra.tool.SchedulerTool;

import java.util.Map;

/**
 * 调度
 */
@JobHandler("DEMO44628")
public class DemoJob implements IJobHandler {
    @Override
    public ReturnT execute(Map<String, String> map, SchedulerTool tool) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        }
        //刷新任务进度及描述
        tool.updateProgress(100, "任务执行中...");
        //任务日志记录
        Long tenantId = tool.getBelongTenantId();
        tool.info("示例任务1执行成功！" + "租户ID为：" + tenantId);
        //获取用户详情
        CustomUserDetails userDetails = DetailsHelper.getUserDetails();
        tool.info("示例任务1执行成功！" + "用户详情为：" + userDetails);
        return ReturnT.SUCCESS;

    }

    @Override
    public void onCreate(SchedulerTool tool) {
        tool.info("在执行任务前初始化数据");
    }

    @Override
    public void onException(SchedulerTool tool) {
        tool.error("出错了");
    }

    @Override
    public void onFinish(SchedulerTool tool, ReturnT returnT) {
        tool.info("结束了");
    }
}